const app = getApp();
const WXAPI = require("apifm-wxapi");
const AUTH = require("../../utils/auth");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    balance: 0.0,
    freeze: 0,
    score: 0,
    score_sign_continuous: 0,
    cashlogs: undefined,
    tab: 1,
    picsList2: [],
    picsList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    AUTH.checkHasLogined().then((isLogined) => {
      if (isLogined) {
        this.doneShow();
      } else {
        wx.showModal({
          title: "提示",
          content: "本次操作需要您的登录授权",
          cancelText: "暂不登录",
          confirmText: "前往登录",
          success(res) {
            if (res.confirm) {
              wx.switchTab({
                url: "/pages/my/index",
              });
            } else {
              wx.navigateBack();
            }
          },
        });
      }
    });
  },
  _tab: function (e) {
    this.setData({
      tab: e.currentTarget.dataset.tab,
    });
  },

  afterPicRead(e) {
    let picsList = this.data.picsList;
    if (!picsList) {
      picsList = [];
    }
    picsList = picsList.concat(e.detail.file);
    this.setData({
      picsList,
    });
  },
  afterPicDel(e) {
    let picsList = this.data.picsList;
    picsList.splice(e.detail.index, 1);
    this.setData({
      picsList,
    });
  },
  previewImage: function (e) {
    const that = this;
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: that.data.files, // 需要预览的图片http链接列表
    });
  },

  afterPicRead2(e) {
    let picsList2 = this.data.picsList2;
    if (!picsList2) {
      picsList2 = [];
    }
    picsList2 = picsList2.concat(e.detail.file);
    this.setData({
      picsList2,
    });
  },
  afterPicDel2(e) {
    let picsList2 = this.data.picsList2;
    picsList2.splice(e.detail.index, 1);
    this.setData({
      picsList2,
    });
  },

  async sub() {
    if (this.data.picsList || this.data.picsList) {
      const extJsonStr = {};
      let temp1 = this.data.picsList;
      let temp2 = this.data.picsList2;
      if (temp1.length > 0) {
        extJsonStr["小票"] = "是";
      }
      if (temp2.length > 0) {
        extJsonStr["支付凭证"] = "是";
      }

      // 批量上传附件
      if (this.data.picsList) {
        for (let index = 0; index < this.data.picsList.length; index++) {
          const pic = this.data.picsList[index];
          const res = await WXAPI.uploadFileV2(
            wx.getStorageSync("token"),
            pic.url
          );
          if (res.code == 0) {
            extJsonStr["file" + index] = res.data.url;
          }
        }
      }

      if (this.data.picsList2) {
        for (let index = 0; index < this.data.picsList2.length; index++) {
          const pic = this.data.picsList2[index];
          const res = await WXAPI.uploadFileV2(
            wx.getStorageSync("token"),
            pic.url
          );
          if (res.code == 0) {
            extJsonStr["file" + index] = res.data.url;
          }
        }
      }

      const res = await WXAPI.addComment({
        token: wx.getStorageSync("token"),
        type: 1,
        extJsonStr: JSON.stringify(extJsonStr),
        content: "消费兑换积分",
      });
      if (res.code == 0) {
        wx.showToast({
          title: "提交成功",
        });
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none",
        });
      }
    }
  },

  doneShow: function () {
    const _this = this;
    const token = wx.getStorageSync("token");
    WXAPI.userAmount(token).then(function (res) {
      if (res.code == 0) {
        _this.setData({
          balance: res.data.balance.toFixed(2),
          freeze: res.data.freeze.toFixed(2),
          totleConsumed: res.data.totleConsumed.toFixed(2),
          score: res.data.score,
        });
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none",
        });
      }
    });
    // 读取积分明细
    WXAPI.scoreLogs({
      token: token,
      page: 1,
      pageSize: 50,
    }).then((res) => {
      if (res.code == 0) {
        _this.setData({
          cashlogs: res.data.result,
        });
      }
    });
  },
  goshop: function (e) {
    wx.navigateTo({
      url: "/pages/score-shop/index",
    });
  },
  recharge: function (e) {
    wx.navigateTo({
      url: "/pages/recharge/index",
    });
  },
  withdraw: function (e) {
    wx.navigateTo({
      url: "/pages/withdraw/index",
    });
  },
});
